package au.com.iglootech.TaxTimeToolTip.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import au.com.iglootech.TaxTimeToolTip.UserSessionData;
import au.com.iglootech.TaxTimeToolTip.Entity.TaxTimeSheetLog;
import au.com.iglootech.TaxTimeToolTip.dao.GenericDao;
import au.com.iglootech.TaxTimeToolTip.service.TaxTimeSheetLogService;
import au.com.iglootech.TaxTimeToolTip.util.DateUtils;

@Component(TaxTimeSheetLogServiceImpl.BEANNAME)
@SuppressWarnings("unchecked")
public class TaxTimeSheetLogServiceImpl implements TaxTimeSheetLogService {
	public static final String BEANNAME = "TaxTimeSheetLogServiceImpl";

	@Autowired
	private transient GenericDao genericDao;
	
	@Override
	public void addLog(String eventName, String description) {
		TaxTimeSheetLog log = new TaxTimeSheetLog();
		log.setTime(new Date());
		log.setEventName(eventName);
		log.setDescription(description);
		log.setUser(UserSessionData.getUserData().getUsername());
		genericDao.save(log);
	}

	@Override
	public List<TaxTimeSheetLog> findAll() {
		
		
		return new ArrayList<TaxTimeSheetLog>(genericDao.findAll(TaxTimeSheetLog.class));
	}

	@Override
	public List<TaxTimeSheetLog> findAll(final Date startDate, final Date endDate) {
		Date pStartDate = startDate == null ? DateUtils.systemStartDate() : startDate;
		Date pEndDate = endDate == null ? DateUtils.systemEndDate() : endDate;
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("pStartDate", pStartDate);
		params.put("pEndDate", pEndDate);
		return new ArrayList<TaxTimeSheetLog>(genericDao.findByNamedQuery(TaxTimeSheetLog.class, "findByDate", params));
	}

}
